global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

capture log close
log using "$log/9_new_hires_history_wide", replace

*Clean panel of new hires
	use if base>=513 & base<1500 using "$temp\new_hires_treat", clear
	keep if salaried==1
	rename yr_month month_hired
	rename client_id client0
	rename state state0
	rename base base0 
	drop bin ot gross salaried
	save "$temp/hires_temp_wide", replace 

*Loop from 2013-01 to 2020-12
cap program drop loop
program define loop 
args year month
clear all 
	
	forvalues i=0/95 {
	    local m=mod(`month'-1+`i',12)+1
		local y=`year'+int((`month'-1+`i')/12)
				
		*Input one month of data
			capture use yr_month state client_id emp_pur_c salaried base ot gross naics using "$temp\cleaned_`y'`m'", clear
			capture use yr_month state client_id emp_pur_c salaried base ot gross naics using "$temp\cleaned_`y'0`m'", clear
			
		*Keep only those that show up as a new hire at some point between 2015-06 to 2018-05
			joinby emp_pur_c using "$temp\hires_temp_wide"
			
		*Dual job holders: For each new hire, keep only one observation - either same the firm or the highest paying job
			gen same_firm = client0==client_id 
			bysort client0 state0 emp_pur_c month_hired: egen max_base=max(base)
			bysort client0 state0 emp_pur_c month_hired: egen match=max(same_firm)
			keep if same_firm==1 | (match==0 & base==max_base)
			drop max_base match same_firm
		
		*Save 
			save "$temp\hires_`y'`m'", replace
	}
	
	*Append hires
	clear all 
	forvalues i=0/95 {
		local m=mod(`month'-1+`i',12)+1
		local y=`year'+int((`month'-1+`i')/12)
	    append using "$temp\hires_`y'`m'"
	}		
	compress
	save "$output\new_hires_panel_wide", replace
	
	*Delete temp files
	forvalues i=0/95 {
		local m=mod(`month'-1+`i',12)+1
		local y=`year'+int((`month'-1+`i')/12)
		erase "$temp\hires_`y'`m'.dta"
	}
end 

loop 2013 1

log close